package com.zee.demo;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.slider.Slider;
import com.zee.digital.human.data.model.BroadcastParams;
import com.zee.digital.human.data.model.BroadcastType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BroadcastSettingsDialog extends Dialog {

    private final Context mContext;
    private final List<String> mItems;
    private final OnConfirmListener mListener;
    private final String mTitle;
    private final String mAvatarCode;
    private final HashMap<Integer, String> selectedItemMap = new HashMap<>();
    private int broadcastType = 0;

    public interface OnConfirmListener {
        void onConfirm(BroadcastParams broadcastParams);
    }

    public BroadcastSettingsDialog(@NonNull Context context, String title, String avatarCode,
                                   List<String> items, OnConfirmListener listener) {
        super(context, R.style.CustomDialogStyle);
        mContext = context;
        mTitle = title;
        mAvatarCode = avatarCode;
        mItems = items;
        mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_broadcast);

        // 设置对话框宽度
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }

        // 初始化视图
        TextView titleTv = findViewById(R.id.dialog_title);
        ListView listView = findViewById(R.id.list_view);

        LinearLayout voiceCodeLayout = findViewById(R.id.ll_voice_code);
        LinearLayout voiceAddressLayout = findViewById(R.id.ll_voice_address);

        EditText broadcastEditText = findViewById(R.id.edit_text_broadcast);
        EditText voiceAddressEditText = findViewById(R.id.edit_text_voice_address);
        EditText voiceCodeEditText = findViewById(R.id.edit_text_voice_code);

        Slider volumeSlider = findViewById(R.id.slide_volume);

        Slider speedSlider = findViewById(R.id.slide_speed);
        LinearLayout speedSliderLayout = findViewById(R.id.ll_slide_speed);

        if (broadcastType == 0) {
            speedSliderLayout.setVisibility(View.VISIBLE);
            voiceCodeLayout.setVisibility(View.VISIBLE);
            voiceAddressLayout.setVisibility(View.GONE);
            broadcastEditText.setHint("欢迎使用ZeeAvatarSDK智能体播报功能，这是一个强大的数字人解决方案，支持文本和音频两种播报模式，让您的英语更具互动性");
        } else {
            speedSliderLayout.setVisibility(View.GONE);
            voiceCodeLayout.setVisibility(View.GONE);
            voiceAddressLayout.setVisibility(View.VISIBLE);
            broadcastEditText.setHint("欢迎使用ZeeAvatarSDK智能体播报功能");
        }

        Button cancelBtn = findViewById(R.id.btn_cancel);
        Button confirmBtn = findViewById(R.id.btn_confirm);

        // 设置标题
        titleTv.setText(mTitle);

        TextView textType = findViewById(R.id.text_type);
        textType.setOnClickListener(view -> {
            if (broadcastType == 0) {
                broadcastType = 1;
                textType.setText("文本播报");
                speedSliderLayout.setVisibility(View.GONE);
                voiceCodeLayout.setVisibility(View.GONE);
                voiceAddressLayout.setVisibility(View.VISIBLE);

            } else {
                broadcastType = 0;
                textType.setText("音频播报");
                speedSliderLayout.setVisibility(View.VISIBLE);
                voiceCodeLayout.setVisibility(View.VISIBLE);
                voiceAddressLayout.setVisibility(View.GONE);
            }
        });

        // 初始化列表适配器
        final ItemAdapter adapter = new ItemAdapter(mContext,
                android.R.layout.simple_list_item_1, mItems);
        listView.setAdapter(adapter);

        // 列表项点击事件
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (selectedItemMap.containsKey(position)){
                selectedItemMap.remove(position);
            } else {
                selectedItemMap.put(position, mItems.get(position));
            }

            adapter.notifyDataSetChanged();
        });

        // 取消按钮
        cancelBtn.setOnClickListener(v -> dismiss());

        // 确认按钮
        confirmBtn.setOnClickListener(v -> {
            String broadcastText = broadcastEditText.getText().toString().trim();
            if (broadcastText.isEmpty()) {
                broadcastText = broadcastEditText.getHint().toString();
            }
            String voiceCodeText = voiceCodeEditText.getText().toString().trim();
            if (voiceCodeText.isEmpty()) {
                voiceCodeText = voiceCodeEditText.getHint().toString();
            }

            String voiceAddressText = voiceAddressEditText.getText().toString().trim();
            if (voiceAddressText.isEmpty()) {
                voiceAddressText = voiceAddressEditText.getHint().toString();
            }

            BroadcastParams.Builder builder = new BroadcastParams.Builder(broadcastType == 0 ? BroadcastType.TEXT : BroadcastType.AUDIO,
                    mAvatarCode,
                    broadcastText,
                    voiceAddressText,
                    voiceCodeText);
            builder.setMotionList(new ArrayList<>(selectedItemMap.values()));
            builder.setVolume(volumeSlider.getValue() * 100);
            builder.setSpeed(speedSlider.getValue());

            if (mListener != null) {
                mListener.onConfirm(builder.build());
            }

            dismiss();

        });
    }

    // 自定义适配器，用于显示选中状态
    private class ItemAdapter extends ArrayAdapter<String> {

        public ItemAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = view.findViewById(android.R.id.text1);

            if (selectedItemMap.containsKey(position)) {
                textView.setBackgroundColor(mContext.getResources().getColor(R.color.selected_item_bg));
                textView.setTextColor(mContext.getResources().getColor(R.color.selected_item_text));
            } else {
                textView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
                textView.setTextColor(mContext.getResources().getColor(android.R.color.black));
            }

            return view;
        }
    }
}

